package com.apesay.interceptor;

import com.alibaba.fastjson.JSON;
import com.apesay.utils.Constant;
import com.apesay.utils.ResponseEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.io.IOException;
import java.util.HashMap;

public class APIInterceptor implements HandlerInterceptor{
	
	protected static Logger logger = LoggerFactory.getLogger(APIInterceptor.class);

	@SuppressWarnings("unchecked")
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		//处理左侧菜单
//		HashMap<String, Menu> menuHashMap=(HashMap<String, Menu>) request.getSession().getAttribute("menuhash");
//		if(menuHashMap!=null&&menuHashMap.containsKey(request.getRequestURI())){
//			request.getSession().setAttribute("currentmenu", menuHashMap.get(request.getRequestURI()));
//		}else if(menuHashMap!=null&&menuHashMap.containsKey(request.getRequestURI().replace("/", ""))){
//			request.getSession().setAttribute("currentmenu", menuHashMap.get(request.getRequestURI().replace("/", "")));
//		}
		return true;
	}

	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		/*
		//这里执行 处理成功后消息处理
		ResponseEntity responseEntity = (ResponseEntity)request.getAttribute(Constant.CLIENT_RESPONSE);
		String uri  = request.getRequestURI();
		if(uri.startsWith("/customerService/refund/updRefundStatus") || uri.startsWith("/payment/refund")){
			userMessageService.refundMessage(responseEntity, request);
			print(response,responseEntity);
		}else{
			printKeegooService(response,responseEntity);
		}*/
	}

	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		
	}
	
	/**
	 * 
	 * print(客户端输出)
	 * @param res
	 * @param responseEntity
	 * void
	 * @exception
	 * @since  1.0.0
	 */
	protected void print(HttpServletResponse res, ResponseEntity responseEntity) {
		res.setCharacterEncoding("UTF-8");
		res.setHeader("Content-type", "application/json;charset="+"UTF-8");
		try {
			logger.info(JSON.toJSON(responseEntity).toString());
			res.getWriter().print(JSON.toJSONString(responseEntity.getResult(), Constant.serializerFeatures));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 
	 * print(客户端输出)
	 * @param res
	 * @param responseEntity
	 * void
	 * @exception
	 * @since  1.0.0
	 */
	protected void printKeegooService(HttpServletResponse res, ResponseEntity responseEntity) {
		res.setCharacterEncoding("utf-8");
		res.setHeader("Content-type", "application/json;charset="+"utf-8");
		try {
			logger.info(JSON.toJSON(responseEntity).toString());
			res.getWriter().print(JSON.toJSONString(responseEntity, Constant.serializerFeatures));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
